package com.example.inoculations.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.inoculations.entity.Admin;
import com.example.inoculations.entity.User;
import com.example.inoculations.entity.dto.LoginDTO;
import com.example.inoculations.resultR.R;
import com.example.inoculations.service.IAdminService;
import com.example.inoculations.utils.JWTUtils;
import com.example.inoculations.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author meirui
 * @since 2022-11-19
 */
@RestController
@RequestMapping("/inoculations/admin")
public class AdminController {
    @Autowired
    IAdminService iAdminService;
    /*分页查询*/
    @GetMapping("/pageall")
    public R<IPage<Admin>> getpageall(@RequestParam("pageNum") Integer pageNum,
                                      @RequestParam("pageSize") Integer pageSize,
                                      @RequestParam(required = false) String adminid,
                                      @RequestParam(required = false) String adminname) {
        IPage<Admin> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        /*根据身份证号模糊查询*/
        if (!StringUtils.isEmpty(adminid)){
            queryWrapper.like("idcard",adminid);
        }
        /*根据姓名模糊查询*/
        if (!StringUtils.isEmpty(adminname)){
            queryWrapper.like("name",adminname);
        }
        IPage<Admin> page1 = iAdminService.page(page, queryWrapper);
        return R.SUCCESS(page1);
    }
    /*添加和修改，根据是否传入id，有则修改，无则添加*/
    @PostMapping("/save")
    public R adminsave(@RequestBody Admin admin){
        if (admin!= null){
            admin.setPassword(MD5Utils.getMD5(admin.getPassword()));
        }
        if (iAdminService.saveOrUpdate(admin)){
            return R.SUCCESS();
        }else {
            return R.FAIL();
        }
    }
    /*删除*/
    @DeleteMapping("/del/{id}")
    public R admindel(@PathVariable Integer id){
        if (iAdminService.removeById(id)){
            return R.SUCCESS();
        }else {
            return R.FAIL();
        }
    }
//    管理员登录
    @PostMapping("/login")
    public R adminLogin(@RequestBody LoginDTO loginDTO){
        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();
        password = MD5Utils.getMD5(password);
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",username);
        Admin admin = iAdminService.getOne(queryWrapper);
        if(admin.getPassword().equals(password)){ //登录成功
            //设置token
            Map<String,String> hm = new HashMap<>();
            hm.put("username",username);
            hm.put("userid",String.valueOf(admin.getId()));
//            System.out.println(String.valueOf(user.getId()));
            String token = JWTUtils.getToken(hm);
            loginDTO.setUserToken(token);
            return R.SUCCESS(loginDTO);
        }else {
            return R.FAIL();
        }

    }
    /**
     * 通过token获取管理员的id然后查询管理员信息
     * @param token
     * @return
     */
    @PostMapping("/getTokenId")
    public R getTokenId(@RequestBody String token){

        //System.out.println(token+"22222");
        String aa = token.replace("\"", "");  //去掉多余的双引号
        // System.out.println(aa);
        String tokenId = JWTUtils.getIdByToken(aa);
       Admin admin = iAdminService.getById(Integer.valueOf(tokenId));
        // System.out.println(user.getName());
        return R.SUCCESS(admin);
    }
}
